import axios, { CancelToken } from './http'
import type { AxiosRes, ResData, AxiosResData } from './http'
import { postSubjectTwoFieldType } from '@/pages/subject_manage/inferface'
import { PersonInfoFieldType } from '@/pages/person_info/interface'
import { ExamList, ExamTopicListDateType, ExamTopicType, GetAdminList, LoginBody, MenuData, Page, SubjectTreeType, TopicData, UserInfo } from './interface'
import { NoteData } from '@/store/slice/note/interface'

// 登录
export async function LoginPost(body: LoginBody) {
  return new Promise<UserInfo>(async (resolve, reject) => {
    const res: AxiosResData<UserInfo> = await axios.post('/user/login', body)
    resolve(res.data.data)
  })
}

// 注册
export async function RegisterPost(body: LoginBody) {
  return new Promise<UserInfo>(async (resolve, reject) => {
    const res: AxiosResData<UserInfo> = await axios.post('/user/register', body)
    resolve(res.data.data)
  })
}

// 修改密码
export async function postUpdatePassword(body: any) {
  return new Promise<any>(async (resolve, reject) => {
    const res: AxiosResData<any> = await axios.post('/user/password', body)
    resolve(res.data.data)
  })
}

// 退出登录
export function LogoutPost() {
  return new Promise(async (reslove, reject) => {
    const res: AxiosResData = await axios.post('/user/logout')
    reslove(res.data.data)
  })
}

// 获取菜单
export function getMenuRequest() {
  return new Promise<MenuData[]>(async (resolve, reject) => {
    const res: AxiosResData<MenuData[]> = await axios.get('/user/menu')
    resolve(res.data.data)
  })
}

// 获取用户信息
export function getUserInfoRequest() {
  return new Promise<UserInfo>(async (resolve, reject) => {
    const res = await axios.get('/user')
    resolve(res.data.data)
  })
}

// 获取阅卷列表
export function postExamList(body: any) {
  return new Promise<ExamList[]>(async (resolve, reject) => {
    const res: AxiosRes<ExamList[]> = await axios.post('/exam', body)
    resolve(res.data)
  })
}

// 获取课程的树型结构
export function getSubjectTree() {
  return new Promise<SubjectTreeType[]>(async (reslove, reject) => {
    //@ts-ignore
    const res: AxiosResData<SubjectTreeType[]> = await axios.get('/subject', { cache: true })
    reslove(res.data.data)
  })
}

// 根据二级课程id查询题目
export function getTopic(s2id: string) {
  return new Promise<TopicData[]>(async (resolve, reject) => {
    const res: AxiosResData<TopicData[]> = await axios.get(`/topic/${s2id}`)
    resolve(res.data.data)
  })
}

// 删除二级课程
export function deleteSubjectTwo(id: string) {
  return new Promise(async (resolve, reject) => {
    const res = await axios.delete(`/subject/two/${id}`)
    resolve(res)
  })
}

// 获取一级课程
// export type SubjectType = {
//   name: string
//   key: string
// }
// export function getSubjectOne() {
//   return new Promise<SubjectType[]>(async (resolve, reject) => {
//     const res: AxiosResData<SubjectType[]> = await axios.get('/subject/one')
//     resolve(res.data.data)
//   })
// }

// 新增二级课程
export function postSubjectTwo(body: postSubjectTwoFieldType) {
  return new Promise(async (resolve, reject) => {
    const res = await axios.post('/subject/two', body)
    resolve(res)
  })
}

// 获取学生列表
export function getStudentList(params: any) {
  return new Promise<ResData<UserInfo[]>>(async (resolve, reject) => {
    const res: AxiosResData<UserInfo[]> = await axios.get('/user/student', {
      params
    })
    resolve(res.data)
  })
}

// 学生权限的修改
export function patchStudentRole(user_id: string, role: []) {
  return new Promise(async (resolve, reject) => {
    const res = await axios.patch(`/user/${user_id}`, {
      subject_role: role
    })
    resolve(res)
  })
}

// 删除学生
export function deleteStudent(id: string) {
  return new Promise(async (resolve, reject) => {
    const res = await axios.delete(`user/student/${id}`)
    resolve(res)
  })
}

// 修改或者填写个人信息
export function patchPersonInfo(user_id: string, params: PersonInfoFieldType) {
  return new Promise<AxiosResData<any>>(async (resolve, reject) => {
    const res: AxiosResData<any> = await axios.patch(`/user/${user_id}`, { ...params })
    resolve(res)
  })
}

// 根据考试ID获取考试列表
export function getExamTopicListById(id: string) {
  return new Promise<ExamTopicType>(async (resolve, reject) => {
    const res: AxiosResData<ExamTopicType> = await axios.get(`/exam/${id}`)
    resolve(res.data.data)
  })
}

// 管理员批阅试卷
export function patchExam(exam_id: string, body: any) {
  return new Promise<AxiosResData<any>>(async (resolve, reject) => {
    const res: AxiosResData<any> = await axios.patch(`/exam/${exam_id}`, body)
    resolve(res)
  })
}

// 获取管理员列表
export function getAdminList(params: GetAdminList) {
  return new Promise<Page<UserInfo[]>>(async (resolve, reject) => {
    const res: AxiosResData<Page<UserInfo[]>> = await axios.get('/user/admin', { params })
    resolve(res.data.data)
  })
}

// 新增管理员
export function postAddAdmin(phone: string) {
  return new Promise(async (resolve, reject) => {
    const res: AxiosResData<any> = await axios.post(`/user/add_admin`, { phone })
    resolve(res)
  })
}

// 删除老师
export function deleteAdmin(id: number) {
  return new Promise(async (resolve, reject) => {
    const res = await axios.delete(`user/admin/${id}`)
    resolve(res)
  })
}

// 获取试卷题目
export function getExamTopic(subject_two_id: string) {
  return new Promise<ExamTopicListDateType[]>(async (resolve, reject) => {
    const res: AxiosResData<ExamTopicListDateType[]> = await axios.get(`/topic/${subject_two_id}`)
    resolve(res.data.data)
  })
}

// 提交试卷
export function postStudentExam(body: { topic_list: ExamTopicListDateType[]; two_id: string }) {
  return new Promise<AxiosResData<any>>(async (resolve, reject) => {
    const res: AxiosResData<any> = await axios.post('/exam/create', body)
    resolve(res)
  })
}

// 新增题目
export function postTopic(body: any) {
  return new Promise<AxiosResData<any>>(async (resolve, reject) => {
    const res: AxiosResData<any> = await axios.post(`/topic`, body)
    resolve(res)
  })
}

// 修改题目
export function patchTopic(id: string, body: any) {
  return new Promise<AxiosResData<any>>(async (resolve, reject) => {
    const res: AxiosResData<any> = await axios.patch(`/topic/${id}`, body)
    resolve(res)
  })
}

// 新增笔记
export function createNewNote(body: any) {
  return new Promise<AxiosResData<any>>(async (resolve, reject) => {
    const res: AxiosResData<any> = await axios.post(`/note`, body)
    resolve(res)
  })
}

// 获取笔记列表
export function getNoteList(params: any) {
  return new Promise<ResData<NoteData[]>>(async (resolve, reject) => {
    const res: AxiosResData<NoteData[]> = await axios.get('/note', {
      params
    })
    resolve(res.data)
  })
}

// 修改笔记
export function patchNote(id: number, body: any) {
  return new Promise(async (resolve, reject) => {
    const res = await axios.patch(`/note/${id}`, body)
    resolve(res)
  })
}

// 删除笔记
export function deleteNote(id: number) {
  return new Promise(async (resolve, reject) => {
    const res = await axios.delete(`/note/${id}`)
    resolve(res)
  })
}

// 获取待办列表
export function getTodoList() {
  return new Promise<ResData<NoteData[]>>(async (resolve, reject) => {
    const res: AxiosResData<NoteData[]> = await axios.get('/todolist')
    resolve(res.data)
  })
}

// 新增待办
export function createTodolist(body: any) {
  return new Promise<AxiosResData<any>>(async (resolve, reject) => {
    const res: AxiosResData<any> = await axios.post(`/todolist`, body)
    resolve(res)
  })
}

// 删除待办
export function deleteTodolist(id: number) {
  return new Promise(async (resolve, reject) => {
    const res = await axios.delete(`/todolist/${id}`)
    resolve(res)
  })
}

// 待办是否完成
export function checkTodolist(id: number, body: any) {
  return new Promise(async (resolve, reject) => {
    const res = await axios.patch(`/todolist/${id}`, body)
    resolve(res)
  })
}

// 获取各个职位的数量
export function getRole() {
  return new Promise(async (resolve, reject) => {
    const res: AxiosResData = await axios.get('user/role')
    resolve(res.data)
  })
}

// 获取各个教育水平的数量
export function getEdu() {
  return new Promise(async (resolve, reject) => {
    const res: AxiosResData = await axios.get('user/edu')
    resolve(res.data)
  })
}

// 获取各个技术栈的数量
export function getTechStack() {
  return new Promise(async (resolve, reject) => {
    const res: AxiosResData = await axios.get('user/techStack')
    resolve(res.data)
  })
}

// 获取试卷是否被批阅的数量
export function getExamCorrect() {
  return new Promise(async (resolve, reject) => {
    const res: AxiosResData = await axios.get('user/correct')
    resolve(res.data)
  })
}

// ai聊天
export function postAIChat(body: any) {
  return new Promise<AxiosResData<any>>(async (resolve, reject) => {
    const res: AxiosResData<any> = await axios.post(`/aichat`, body)
    resolve(res)
  })
}

// 检查md5文件是否存在(存在就秒传)
export function getMd5Exist(md5: string, fileName: string) {
  return new Promise<AxiosResData<any>>(async (resolve, reject) => {
    const res: AxiosResData<any> = await axios.get(`/upload/check/file?md5=${md5}&fileName=${fileName}`)
    resolve(res)
  })
}

// 分片上传
// 取消请求
export let chunks_upload_cancel: any
// CancelToken.source() 创建了一个取消令牌源
// token 用于标识请求，cancel 是一个函数，用于取消带有该 token 的请求。
let source = CancelToken.source()
export function postUploadChunk(body: any) {
  return new Promise<AxiosResData<any>>(async (resolve, reject) => {
    const res: AxiosResData<any> = await axios.post(`upload/chunk/upload`, body, {
      // 取消请求=====这个 token 是一个用于标识该请求的令牌。
      cancelToken: source.token
    })
    resolve(res)
  })
}

// 取消请求====取消带有source.token标志的请求
export const cancelChunkUpload = () => {
  // 调用 CancelToken 实例的 cancel() 方法即可取消对应的请求。
  source.cancel('取消上传')
  source = CancelToken.source()
}

// 合并分片
export function getMerge(md5: string, fileName: string, total: number, chunkSize: number) {
  return new Promise<AxiosResData<any>>(async (resolve, reject) => {
    const res: AxiosResData<any> = await axios.get(`upload/chunk/merge?md5=${md5}&fileName=${fileName}&total=${total}&chunkSize=${chunkSize}`)
    resolve(res)
  })
}
